// college.js
const collegeApp = createApp({
    data() {
        return {
            colleges: [],
            loading: false,
            error: null,
            formData: {
                collegeCode: '',
                collegeName: ''
            }
        }
    },
    methods: {
        // 加载学院数据
        async loadColleges() {
            this.loading = true;
            this.error = null;
            try {
                const response = await fetch(window.contextPath+'/college/listAll');
                if (response.ok) {
                    this.colleges = await response.json();
                } else {
                    const errorText = await response.text();
                    this.error = '加载学院数据失败,错误码:'+ response.status +',错误信息: ' + errorText
                    showErrorModal(this.error)
                }
            }catch ( error) {
                this.error = '网络错误: ' + error.message;
                showErrorModal(this.error)
            }finally {
                this.loading = false;
            }
        },
        // 添加学院信息
        async addCollege() {
            try {
                const response = await fetch(window.contextPath+'/college/add', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(this.formData)
                });

                if (response.ok) {
                    alert('添加成功')
                    await this.loadColleges();
                    this.formData = {
                        collegeCode: '',
                        collegeName: ''
                    };
                    closeCollegeAddModal();
                } else {
                    const errorText = await response.text();
                    this.error = '添加学院信息失败,错误码:'+ response.status +',错误信息: ' + errorText
                    showErrorModal(this.error)
                }
            } catch (error) {
                this.error = '网络错误: ' + error.message;
                showErrorModal(this.error)
            }
        },
        // 删除学院信息
        async deleteCollege(collegeCode) {
            try {
                if (!confirm('确定要删除该学院信息吗？')) {
                    return;
                }
                const response = await fetch(window.contextPath+'/college/delete?collegeCode=' + collegeCode, {
                    method: 'DELETE'
                });
                if (response.ok) {
                    alert('删除成功')
                    await this.loadColleges();
                } else {
                    const errorText = await response.text();
                    this.error = '删除学院失败,错误码:'+ response.status +',错误信息: ' + errorText
                    showErrorModal(this.error)
                }
            } catch (error) {
                this.error = '网络错误: ' + error.message;
                showErrorModal(this.error)
            }
        },
        // 修改学院信息
        async updateCollege() {
            alert('该功能正在开发中...')
        }
    },
    mounted() {
        window.collegeAppInstance = this;
    }
});
collegeApp.mount('#college-content');

// 显示添加学院表单
function showCollegeAddModal() {
    const modal = document.getElementById('collegeAddModal');
    modal.classList.add('show');
    modal.style.display = 'flex';
}

// 关闭添加学院表单
function closeCollegeAddModal() {
    document.getElementById('collegeAddModal').style.display = 'none';
}
